let seed = 1;

export async function loadRemoteSvg(id, remoteUrl: string) {
  switch (arguments.length) {
    case 1:
      remoteUrl = id;
      id = "remote-svg-file-" + seed++;
      break;
    case 2:
      // 判断是否已经进行过加载了
      if (document.getElementById(id)) {
        return;
      }
      break;
    default:
      return;
  }
  // 判断是否有提供远程加载的地址
  if (!remoteUrl) {
    return;
  }

  const response = await fetch(remoteUrl);
  const text = await response.text();
  const svgWrap = document.createElement("div");
  svgWrap.innerHTML = text;
  const svg = svgWrap.firstElementChild as HTMLElement;
  svg.style.display = "none";
  svg.id = id;
  svg.hidden = true;
  // document.body.insertBefore(svg, document.body.children[0]);
  document.body.appendChild(svg);

  return id;
}
